package com.zdp.chat.enums;

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;


/**
 * 消息类型
 * 消息体参考
 * @author Administrator
 */

@AllArgsConstructor
@Getter
public enum ContextTypeEnum {
    TEXT(1, "正常消息"),
    RECALL(2, "撤回消息"),
    IMG(3, "图片"),
    FILE(4, "文件"),
    SOUND(5, "语音"),
    VIDEO(6, "视频"),
    EMOJI(7, "表情"),
    SYSTEM(8, "系统消息"),
    IMG_TEXT(9, "图文消息"),
    ;

    private final Integer type;
    private final String desc;

    private static Map<Integer, ContextTypeEnum> cache;

    static {
        cache = Arrays.stream(ContextTypeEnum.values()).collect(Collectors.toMap(ContextTypeEnum::getType, Function.identity()));
    }

    public static ContextTypeEnum of(Integer type) {
        return cache.get(type);
    }

    /**
     * 获取消息通道key
     *
     * @param type  消息类型
     * @return  结果
     */
    public static String getMsgKey(Integer type){
        ContextTypeEnum of = of(type);
        if(Objects.isNull(of)){
            throw new RuntimeException("消息类型错误！");
        }
        return RedisKeyConstant.MSG_TOPIC + of.name().toLowerCase();
    }
}
